package nancal.mp.client

import nancal.mp.client.iam.IamAdminApiTokenClient
import nancal.mp.client.mp.IntegrationClient
import nbcp.base.comm.config
import nbcp.base.extend.AsString
import nbcp.base.utils.SpringUtil
import nbcp.http.api.DockerClientApi
import org.slf4j.LoggerFactory

object api {
    object iam {
        val iamAdmin by lazy {
            return@lazy SpringUtil.getBean<IamAdminApiTokenClient>()
        }
    }

    object mp {
        val integration by lazy {
            return@lazy SpringUtil.getBean<IntegrationClient>()
        }
    }

    val lock = Any();

    object sys {
        private var dockerFeign: DockerClientApi? = null

        val docker: DockerClientApi?
            get() {
                if (config.getConfig("app.run-env").AsString() != "docker") {
                    return null;
                }
                if (dockerFeign == null) {
                    synchronized(lock) {
                        try {
                            if (SpringUtil.containsBean(DockerClientApi::class.java)) {
                                dockerFeign = SpringUtil.getBean<DockerClientApi>()
                            }
                        } catch (e: Exception) {
                            LoggerFactory.getLogger(sys::class.java).error("获取 DockerClientApi 失败！${e.message}")
                        }
                    }
                }
                return dockerFeign;
            }
    }
}